// pages/classifyDetail/classifyDetail.js
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 商品列表
    goodsList:[],
    dTitle:String,
    categoryId:String,
    count:Number,
    page:1
  },

  clickDetails(e) {
    console.log(e)
    wx.navigateTo({
      url: '../goodsDetails/details?id=' + e.currentTarget.dataset.id
    })
  },
  // 获取详情列表数据
  getClassDetails(){
    console.log(this)
    let categoryId = this.options.categoryId;
    let dTitle = this.options.dTitle;
    console.log(dTitle)
    console.log(categoryId)
    wx.request({
      url: api.goodsList,
      data:{
        categoryId: categoryId
      },
      success:res=>{
        console.log(res);
        let goodsList = res.data.data.goodsList;
        let count = res.data.data.count;
        this.setData({
          goodsList,
          dTitle,
          categoryId,
          count
        })
      }
    })
  },
  // 上下页数据请求
  changeData(e){
    let count=this.data.count;
    let page=this.data.page;
    console.log(e)
    if (e._relatedInfo.anchorTargetText=='上一页'){
      console.log(page)
      if(page>1){
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 300
        })
        --this.data.page;
        wx.request({
          url: api.goodsList,
          data:{
            categoryId: this.data.categoryId,
            page: this.data.page,
            size:10
          },
          success:res=>{
            console.log('上一页')
            console.log(res);
            this.setData({
              page,
              goodsList: res.data.data.goodsList
            })
          }
        })
      }
    }else{
      console.log(count / 10)
      if(page<(count/10)){
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 300
        })
        ++this.data.page;
        wx.request({
          url: api.goodsList,
          data: {
            categoryId: this.data.categoryId,
            page: this.data.page
          },
          success: res => {
            console.log('下一页')
            console.log(res);
            this.setData({
              page:page+1,
              goodsList: res.data.data.goodsList
            })
          }
        })
      }
      else{
        wx.showToast({
          title: '到底啦',
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getClassDetails();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})